<template>
  <el-dialog
    :title="!dataForm.workerNo ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="任务编号" prop="workId">
      <el-input v-model="dataForm.workId" placeholder="任务编号"></el-input>
    </el-form-item>
    <el-form-item label="工人编号" prop="workerId">
      <el-input v-model="dataForm.workerId" placeholder="工人编号"></el-input>
    </el-form-item>
    <el-form-item label="任务类型" prop="workerType">
      <el-input v-model="dataForm.workerType" placeholder="任务类型"></el-input>
    </el-form-item>
    <el-form-item label="工位" prop="workerStationid">
      <el-input v-model="dataForm.workerStationid" placeholder="工位"></el-input>
    </el-form-item>
    <el-form-item label="工人任务描述" prop="workerDescription">
      <el-input v-model="dataForm.workerDescription" placeholder="工人任务描述"></el-input>
    </el-form-item>
    <el-form-item label="未开始/进行中/已完成" prop="workerState">
      <el-input v-model="dataForm.workerState" placeholder="未开始/进行中/已完成"></el-input>
    </el-form-item>
    <el-form-item label="实际开始时间" prop="workerStarttime">
      <el-input v-model="dataForm.workerStarttime" placeholder="实际开始时间"></el-input>
    </el-form-item>
    <el-form-item label="实际结束时间" prop="workerOvertime">
      <el-input v-model="dataForm.workerOvertime" placeholder="实际结束时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          workerNo: 0,
          workId: '',
          workerId: '',
          workerType: '',
          workerStationid: '',
          workerDescription: '',
          workerState: '',
          workerStarttime: '',
          workerOvertime: ''
        },
        dataRule: {
          workId: [
            { required: true, message: '任务编号不能为空', trigger: 'blur' }
          ],
          workerId: [
            { required: true, message: '工人编号不能为空', trigger: 'blur' }
          ],
          workerType: [
            { required: true, message: '任务类型不能为空', trigger: 'blur' }
          ],
          workerStationid: [
            { required: true, message: '工位不能为空', trigger: 'blur' }
          ],
          workerDescription: [
            { required: true, message: '工人任务描述不能为空', trigger: 'blur' }
          ],
          workerState: [
            { required: true, message: '未开始/进行中/已完成不能为空', trigger: 'blur' }
          ],
          workerStarttime: [
            { required: true, message: '实际开始时间不能为空', trigger: 'blur' }
          ],
          workerOvertime: [
            { required: true, message: '实际结束时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.workerNo = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.workerNo) {
            this.$http({
              url: this.$http.adornUrl(`/workerplan/workerplan/info/${this.dataForm.workerNo}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.workId = data.workerplan.workId
                this.dataForm.workerId = data.workerplan.workerId
                this.dataForm.workerType = data.workerplan.workerType
                this.dataForm.workerStationid = data.workerplan.workerStationid
                this.dataForm.workerDescription = data.workerplan.workerDescription
                this.dataForm.workerState = data.workerplan.workerState
                this.dataForm.workerStarttime = data.workerplan.workerStarttime
                this.dataForm.workerOvertime = data.workerplan.workerOvertime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/workerplan/workerplan/${!this.dataForm.workerNo ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'workerNo': this.dataForm.workerNo || undefined,
                'workId': this.dataForm.workId,
                'workerId': this.dataForm.workerId,
                'workerType': this.dataForm.workerType,
                'workerStationid': this.dataForm.workerStationid,
                'workerDescription': this.dataForm.workerDescription,
                'workerState': this.dataForm.workerState,
                'workerStarttime': this.dataForm.workerStarttime,
                'workerOvertime': this.dataForm.workerOvertime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
